import time
from flask import Flask as _Flask, jsonify
from flask import request
from flask import render_template
from flask.json import JSONEncoder as _JSONEncoder
from jieba.analyse import extract_tags
import decimal
import string
import utils


class JSONEncoder(_JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return float(o)
        super(_JSONEncoder, self).default(o)


class Flask(_Flask):
    json_encoder = JSONEncoder


app = Flask(__name__)


@app.route('/')
def hello_word1():
    return render_template("index.html")


@app.route('/index.html')
def hello_word2():
    return render_template("index.html")


@app.route('/yq')
def get_yq_data():
    data = utils.get_yq_data()
    return jsonify({"confirmedRelative": data[0], "asymptomaticRelative": data[1], "overseasInputRelative": data[2],
                    "nativeRelative": data[3], "curConfirm": data[4], "curLocalConfirm": data[5],
                    "curOverseasInput": data[6], "asymptomatic": data[7]
                    })


@app.route('/c_con')
def get_cmp_data():
    res = []
    for tup in utils.get_confirmedRelative_data():
        res.append({"name": tup[0], "value": tup[1]})
    return jsonify({"data": res})


@app.route("/c_con_2")
def get_cmp2_data():
    res = []
    for tup in utils.get_curConfirm_data():
        res.append({"name": tup[0], "value": tup[1]})
    return jsonify({"data": res})


@app.route("/c_con_3")
def get_cmp3_data():
    res = []
    for tup in utils.get_confirmed_data():
        res.append({"name": tup[0], "value": tup[1]})
    return jsonify({"data": res})


@app.route('/echart2')
def get_echart2_data():
    data = utils.get_echart2_data()
    city = []
    confirm_add = []
    for k, v, m in data:
        temp = k + v
        city.append(temp)
        confirm_add.append(m)
    return jsonify({"city": city, "confirm_add": confirm_add})


@app.route('/echart3')
def get_echart3_data():
    data = utils.get_echart3_data()
    day, confirm, supect, heal, dead = [], [], [], [], []
    for a, b, c, d, e in data[::-1]:
        day.append(a.strftime("%m-%d"))
        confirm.append(b)
        supect.append(c)
        heal.append(d)
        dead.append(e)
    return jsonify({"day": day, "confirm": confirm, "supect": supect, "heal": heal, "dead": dead})


@app.route('/echart4')
def get_echart4_data():
    data1, data2 = utils.get_echart4_data()
    local = data1[0][0]
    foreign = data2[0][0] - data1[0][0]
    return jsonify({"local": local, "foreign": foreign})


@app.route('/echart5')
def get_echart5_data():
    data1, data2 = utils.get_echart5_data()
    city = []
    h = []
    m = []

    for i in range(len(data1)):
        city.append(data1[i][0])
    for j in range(len(data2)):
        if data2[j][0] in city:
            continue
        city.append(data2[j][0])
    for i in range(len(city)):
        h.append(utils.get_C(city[i])[1][0][0])
        m.append(utils.get_C(city[i])[0][0][0])
    return jsonify({"city": city, "h": h, "m": m})


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debug=True, threaded=True)
